From 00d0d90fdac9257ed02e24677ba4f008f2486c94 Mon Sep 17 00:00:00 2001
From: phunkyfish <phunkyfish@gmail.com>
Date: Wed, 7 Oct 2020 18:00:57 +0100
Subject: [PATCH] Remove SAFE_DELETE

Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
---
 src/lib/tsreader/FileReader.cpp      |  9 ++++++++-
 src/lib/tsreader/MemoryBuffer.cpp    | 22 +++++++++++++---------
 src/lib/tsreader/MultiFileReader.cpp | 11 ++++++++++-
 src/lib/tsreader/TSReader.cpp        | 25 +++++++++++++++++--------
 src/pvrclient-mediaportal.cpp        | 27 ++++++++++++++++++---------
 src/utils.h                          |  2 --
 6 files changed, 66 insertions(+), 30 deletions(-)

diff --git a/src/lib/tsreader/FileReader.cpp b/src/lib/tsreader/FileReader.cpp
index ac8725c..3df22f2 100644
--- a/src/lib/tsreader/FileReader.cpp
+++ b/src/lib/tsreader/FileReader.cpp
@@ -38,7 +38,6 @@
 #include "p8-platform/threads/threads.h"
 #include <algorithm> //std::min, std::max
 #include "p8-platform/util/timeutils.h" // for usleep
-#include "p8-platform/util/util.h"
 #include "utils.h"
 #include <errno.h>
 
@@ -58,6 +57,14 @@
 /* calcuate bitrate for file while reading */
 #define READ_BITRATE   0x10
 
+template<typename T> void SafeDelete(T*& p)
+{
+  if (p)
+  {
+    delete p;
+    p = nullptr;
+  }
+}
 namespace MPTV
 {
     FileReader::FileReader() :
diff --git a/src/lib/tsreader/MemoryBuffer.cpp b/src/lib/tsreader/MemoryBuffer.cpp
index 6d411d7..609b575 100644
--- a/src/lib/tsreader/MemoryBuffer.cpp
+++ b/src/lib/tsreader/MemoryBuffer.cpp
@@ -31,7 +31,6 @@
 
 #include "p8-platform/util/timeutils.h"
 #include "p8-platform/threads/mutex.h"
-#include "p8-platform/util/util.h"
 #include "MemoryBuffer.h"
 #include <kodi/General.h> //for kodi::Log
 #include "TSDebug.h"
@@ -59,11 +58,14 @@ void CMemoryBuffer::Clear()
   P8PLATFORM::CLockObject BufferLock(m_BufferLock);
   std::vector<BufferItem *>::iterator it = m_Array.begin();
 
-  for ( ; it != m_Array.end(); ++it )
+  for (auto& item : m_Array)
   {
-    BufferItem *item = *it;
-    SAFE_DELETE_ARRAY(item->data);
-    SAFE_DELETE(item);
+    if (item)
+    {
+      if (item->data)
+        delete[] item->data;
+      delete item;
+    }
   }
 
   m_Array.clear();
@@ -150,8 +152,9 @@ size_t CMemoryBuffer::ReadFromBuffer(unsigned char *pbData, size_t lDataLength)
     if (item->nOffset >= item->nDataLength)
     {
       m_Array.erase(m_Array.begin());
-      SAFE_DELETE_ARRAY(item->data);
-      SAFE_DELETE(item);
+      if (item->data)
+        delete[] item->data;
+      delete item;
     }
   }
   return bytesWritten;
@@ -182,8 +185,9 @@ long CMemoryBuffer::PutBuffer(unsigned char *pbData, size_t lDataLength)
 
       m_BytesInBuffer -= copyLength;
       m_Array.erase(m_Array.begin());
-      SAFE_DELETE_ARRAY(item2->data);
-      SAFE_DELETE(item2);
+      if (item2->data)
+        delete[] item2->data;
+      delete item2;
     }
     if (m_BytesInBuffer > 0)
     {
diff --git a/src/lib/tsreader/MultiFileReader.cpp b/src/lib/tsreader/MultiFileReader.cpp
index b8005b3..3828e06 100644
--- a/src/lib/tsreader/MultiFileReader.cpp
+++ b/src/lib/tsreader/MultiFileReader.cpp
@@ -48,6 +48,15 @@ using namespace P8PLATFORM;
 //Maximum time in msec to wait for the buffer file to become available - Needed for DVB radio (this sometimes takes some time)
 #define MAX_BUFFER_TIMEOUT 1500
 
+template<typename T> void SafeDelete(T*& p)
+{
+  if (p)
+  {
+    delete p;
+    p = nullptr;
+  }
+}
+
 namespace MPTV
 {
     MultiFileReader::MultiFileReader() :
@@ -484,7 +493,7 @@ namespace MPTV
 
                 TSDEBUG(ADDON_LOG_DEBUG, "MultiFileReader: Removing file %s\n", file->filename.c_str());
 
-                SAFE_DELETE(file);
+                SafeDelete(file);
                 m_tsFiles.erase(m_tsFiles.begin());
 
                 filesToRemove--;
diff --git a/src/lib/tsreader/TSReader.cpp b/src/lib/tsreader/TSReader.cpp
index 3f9725b..4a72f4f 100644
--- a/src/lib/tsreader/TSReader.cpp
+++ b/src/lib/tsreader/TSReader.cpp
@@ -44,6 +44,15 @@
 
 using namespace std;
 
+template<typename T> void SafeDelete(T*& p)
+{
+  if (p)
+  {
+    delete p;
+    p = nullptr;
+  }
+}
+
 namespace MPTV
 {
     CTsReader::CTsReader() : m_demultiplexer(*this),
@@ -69,10 +78,10 @@ namespace MPTV
 
     CTsReader::~CTsReader(void)
     {
-        SAFE_DELETE(m_fileReader);
+        SafeDelete(m_fileReader);
 #ifdef LIVE555
-        SAFE_DELETE(m_buffer);
-        SAFE_DELETE(m_rtspClient);
+        SafeDelete(m_buffer);
+        SafeDelete(m_rtspClient);
 #endif
     }
 
@@ -225,8 +234,8 @@ namespace MPTV
 
             if ( !m_rtspClient->OpenStream(m_fileName.c_str()) )
             {
-                SAFE_DELETE(m_rtspClient);
-                SAFE_DELETE(m_buffer);
+                SafeDelete(m_rtspClient);
+                SafeDelete(m_buffer);
                 return E_FAIL;
             }
 
@@ -318,8 +327,8 @@ namespace MPTV
 #ifdef LIVE555
                 kodi::Log(ADDON_LOG_INFO, "TsReader: closing RTSP client");
                 m_rtspClient->Stop();
-                SAFE_DELETE(m_rtspClient);
-                SAFE_DELETE(m_buffer);
+                SafeDelete(m_rtspClient);
+                SafeDelete(m_buffer);
 #endif
             }
             else
@@ -327,7 +336,7 @@ namespace MPTV
                 kodi::Log(ADDON_LOG_INFO, "TsReader: closing file");
                 m_fileReader->CloseFile();
             }
-            SAFE_DELETE(m_fileReader);
+            SafeDelete(m_fileReader);
             m_State = State_Stopped;
         }
     }
diff --git a/src/pvrclient-mediaportal.cpp b/src/pvrclient-mediaportal.cpp
index fdb31e7..5479950 100644
--- a/src/pvrclient-mediaportal.cpp
+++ b/src/pvrclient-mediaportal.cpp
@@ -42,6 +42,15 @@ int g_iTVServerKodiBuild = 0;
 #define TVSERVERKODI_RECOMMENDED_VERSION_STRING "1.2.3.122 till 1.20.0.140"
 #define TVSERVERKODI_RECOMMENDED_VERSION_BUILD  140
 
+template<typename T> void SafeDelete(T*& p)
+{
+  if (p)
+  {
+    delete p;
+    p = nullptr;
+  }
+}
+
 /************************************************************/
 /** Class interface */
 
@@ -75,10 +84,10 @@ cPVRClientMediaPortal::~cPVRClientMediaPortal()
   kodi::Log(ADDON_LOG_DEBUG, "->~cPVRClientMediaPortal()");
   Disconnect();
 
-  SAFE_DELETE(Timer::lifetimeValues);
-  SAFE_DELETE(m_tcpclient);
-  SAFE_DELETE(m_genretable);
-  SAFE_DELETE(m_lastSelectedRecording);
+  SafeDelete(Timer::lifetimeValues);
+  SafeDelete(m_tcpclient);
+  SafeDelete(m_genretable);
+  SafeDelete(m_lastSelectedRecording);
 }
 
 string cPVRClientMediaPortal::SendCommand(const char* command)
@@ -321,7 +330,7 @@ void cPVRClientMediaPortal::Disconnect()
       if ((CSettings::Get().GetStreamingMethod()==TSReader) && (m_tsreader != NULL))
       {
         m_tsreader->Close();
-        SAFE_DELETE(m_tsreader);
+        SafeDelete(m_tsreader);
       }
       SendCommand("StopTimeshift:\n");
     }
@@ -1748,7 +1757,7 @@ bool cPVRClientMediaPortal::OpenLiveStream(const kodi::addon::PVRChannel& channe
     m_iCurrentChannel = -1;
     if (m_tsreader != nullptr)
     {
-      SAFE_DELETE(m_tsreader);
+      SafeDelete(m_tsreader);
     }
     return false;
   }
@@ -1959,7 +1968,7 @@ void cPVRClientMediaPortal::CloseLiveStream(void)
     if (CSettings::Get().GetStreamingMethod() == TSReader && m_tsreader)
     {
       m_tsreader->Close();
-      SAFE_DELETE(m_tsreader);
+      SafeDelete(m_tsreader);
     }
     result = SendCommand("StopTimeshift:\n");
     kodi::Log(ADDON_LOG_INFO, "CloseLiveStream: %s", result.c_str());
@@ -2141,7 +2150,7 @@ void cPVRClientMediaPortal::CloseRecordedStream(void)
   {
     kodi::Log(ADDON_LOG_INFO, "CloseRecordedStream: Stop TSReader...");
     m_tsreader->Close();
-    SAFE_DELETE(m_tsreader);
+    SafeDelete(m_tsreader);
   }
   else
   {
@@ -2409,7 +2418,7 @@ cRecording* cPVRClientMediaPortal::GetRecordingInfo(const kodi::addon::PVRRecord
     {
       return m_lastSelectedRecording;
     }
-    SAFE_DELETE(m_lastSelectedRecording);
+    SafeDelete(m_lastSelectedRecording);
   }
 
   if (!IsUp())
diff --git a/src/utils.h b/src/utils.h
index 823b46e..d03569a 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -11,13 +11,11 @@
 #include <vector>
 #include <ctime>
 #include "uri.h"
-#include "p8-platform/util/util.h"
 
 #ifdef TARGET_WINDOWS
 #include "windows/WindowsUtils.h"
 #endif
 
-
 /**
  * String tokenize
  * Split string using the given delimiter into a vector of substrings
